package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.api.article.AuthorControllerApi;
import com.heima.article.service.AuthorService;
import com.heima.common.exception.CostomException;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author feng js
 */
@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {
    @Autowired
    AuthorService authorService;

    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findById(@PathVariable("id") Integer id) {
        ApAuthor user = authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getId, id));
        if (user != null) {
            return user ;
        }
        return null  ;
    }

    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {

        if (apAuthor == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        authorService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @GetMapping("/one/{id}")
    @Override
    public ApAuthor findByIdToUser(@PathVariable("id") Integer id) {
        return authorService.getById(id);
    }
}
